package com.hugesoft.logic.managers.busmanager;

import java.util.List;

import android.os.Handler;

import com.hugesoft.commonlogic.dao.DbManager;
import com.hugesoft.logic.managers.base.BaseBusinessLogicManager;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.logic.module.Bus;
import com.hugesoft.logic.module.BusDao;

import de.greenrobot.dao.query.QueryBuilder;

public class BusManager extends BaseBusinessLogicManager implements IBusManagerInterface{

	public void addbusinfo(Bus bus,Handler handler)
	{
		try {
			bus.setId(null);
			DbManager.getBusDao().insert(bus);
			
			handleaddbusresponse(true,handler);
		} catch (Exception e) {
			handleaddbusresponse(false, handler);
		}
	}
	
	@Override
	public void delbusinfo(String platenumber, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getbusinfo(String platenumber, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getbusinfowithrecordid(Long id, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delbusinfowithrecordid(Long id, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void modifybusinfowithrecordid(Long id, Handler handler) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Bus> loadbusinfo(String platenumber) {
		List<Bus> ret = null;
		QueryBuilder<Bus> qBuilder = DbManager.getBusDao()
				.queryBuilder();
		qBuilder.orderDesc(BusDao.Properties.Checkdate);
		qBuilder.where(
				BusDao.Properties.Platenumber.eq(platenumber));

		ret = qBuilder.list();
		
		return ret;
	}

	private void handleaddbusresponse(boolean isSuccess,Handler handler)
	{
		handleresponse(isSuccess?Constans.ADD_BUS_SUCCESS:Constans.ADD_BUS_FAILED, null, handler);
	}
	
	public Bus loadbusinfo(Long busid)
	{
		Bus ret = null;
		QueryBuilder<Bus> qBuilder = DbManager.getBusDao()
				.queryBuilder();
		qBuilder.where(
				BusDao.Properties.Id.eq(busid));
		qBuilder.orderAsc(BusDao.Properties.Checkdate);

		ret = qBuilder.unique();
		return ret;
	}
	
	public List<Bus> loadallbus()
	{
		List<Bus> ret = null;
		QueryBuilder<Bus> qBuilder = DbManager.getBusDao().queryBuilder();
		qBuilder.orderDesc(BusDao.Properties.Checkdate);
		ret = qBuilder.list();
		return ret;
	}
}
